﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BUS;
namespace GUI
{
    public partial class uc_TaiKhoan : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_TaiKhoan()
        {
            InitializeComponent();
            btnLuu.Enabled = false;
        }

        private AccountDTO acc = new AccountDTO();
        private AccountBL accBL = new AccountBL();

        private void GetAccountInfo()
        {
            if (txtMaTK.Text != "")
                acc.AccountID = int.Parse(txtMaTK.Text);
            if (cbbChucDanh.SelectedIndex == 0)
                acc.Type = 1;
            else acc.Type = 0;
            acc.Name = txtHoTen.Text;
            acc.Password = txtMatKhau.Text;
            acc.Phone = txtSoDienThoai.Text;
            acc.Username = txtUserName.Text;
        }
        private void uc_TaiKhoan_Load(object sender, EventArgs e)
        {
            accountTableAdapter.Fill(sellingmobilemanagementDataSet4.account);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if(radioGroup1.SelectedIndex != 0)
            {
                if (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Type").ToString() == "1")
                {
                    cbbChucDanh.Text = "Admin";
                }
                else
                {
                    cbbChucDanh.Text = "User";
                }
                 txtHoTen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Name").ToString();
                 txtMaTK.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "AccountID").ToString();
                 txtMatKhau.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Password").ToString();
                 txtSoDienThoai.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Phone").ToString();
                 txtUserName.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "UserName").ToString();
            }
        }

        private void txtMaTK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                acc = accBL.Custummer_SelectByID(int.Parse(txtMaTK.Text));
                if (acc.Name == "")
                    MessageBox.Show("Không tìm thấy tài khoản");
                else
                {
                    if (acc.Type == 1)
                        cbbChucDanh.Text = "Admin";
                    else cbbChucDanh.Text = "User";
                     txtHoTen.Text = acc.Name;
                     txtMatKhau.Text = acc.Password;
                     txtSoDienThoai.Text = acc.Phone;
                     txtUserName.Text = acc.Username;
                   }
            }
            if (!char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;

            }
            Constraint.checkLenght(txtSoDienThoai, e, 5);

        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (radioGroup1.SelectedIndex == 0)
            {
                btnThem.Enabled = true;
                btnLuu.Enabled = false;
                txtMaTK.Enabled = false;
            }
            else
            {
                btnLuu.Enabled = true;
                txtMaTK.Enabled = true;
                btnThem.Enabled = false;
            }

            
            txtMaTK.Text = "";
            txtHoTen.Text = "";
            txtMatKhau.Text = "";
            txtSoDienThoai.Text = "";
            txtUserName.Text = "";
            textMatKhau1.Text = "";
            
               
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaTK.Text == "" ||
               txtHoTen.Text == "" ||
               txtMatKhau.Text == "" ||
               txtSoDienThoai.Text == "" ||
               txtUserName.Text == "" ||
               textMatKhau1.Text == "" ||
                txtHoTen.Text == "")
                MessageBox.Show("Vui lòng điền đầy đủ thông tin");
            if (txtMatKhau.Text == textMatKhau1.Text)
            {
                GetAccountInfo();
                accBL.Account_Insert(acc);
                MessageBox.Show("Thêm tài khoản thành công");
                accountTableAdapter.Fill(sellingmobilemanagementDataSet4.account);
            }
            else
                MessageBox.Show("Mật khẩu không khớp");

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if(txtMaTK.Text == "")
                MessageBox.Show("Vui lòng chọn tài khoản cần xóa");

            if (MessageBox.Show("Xóa Tài Khoản?", "Confirmation", MessageBoxButtons.YesNo) ==
               DialogResult.Yes)
            {
                GetAccountInfo();
                accBL.Account_DeleteByID(acc);
                MessageBox.Show("Xóa Thành Công");
                accountTableAdapter.Fill(sellingmobilemanagementDataSet4.account);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {           
            if (MessageBox.Show("Cập Nhật Tài Khoản?", "Confirmation", MessageBoxButtons.YesNo) ==
               DialogResult.Yes)
            {
                GetAccountInfo();
                accBL.Account_Update(acc);
                MessageBox.Show("Cập Nhật Thành Công");
                accountTableAdapter.Fill(sellingmobilemanagementDataSet4.account);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            cbbChucDanh.Text = "";
            txtMaTK.Text = "";
            txtHoTen.Text = "";
            txtMatKhau.Text = "";
            txtSoDienThoai.Text = "";
            txtUserName.Text = "";
            textMatKhau1.Text = "";
        }

        private void txtSoDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;

            }
            Constraint.checkLenght(txtSoDienThoai, e, 11);
        }

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            Constraint.checkLenght(txtHoTen, e, 30);

            Constraint.checkLenght(txtUserName, e, 30);

            Constraint.checkLenght(txtMatKhau, e, 30); 

            Constraint.checkLenght(textMatKhau1, e, 30);

        }
    }
}
